package org.jfbmonitor.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.jfbmonitor.beans.DataBase;
import org.jfbmonitor.connection.FBConnection;
import org.jfbmonitor.language.Language;

public class DataBaseDao {
	private Statement statement;
	private ResultSet resultSet;
	
	public List<DataBase> getAll() throws SQLException{		
		StringBuffer sql = new StringBuffer();		
		sql.append("SELECT");
		sql.append("   MON$DATABASE_NAME,");
		sql.append("   MON$PAGE_SIZE,");
		sql.append("   MON$ODS_MAJOR,");
		sql.append("   MON$ODS_MINOR,");
		sql.append("   MON$OLDEST_TRANSACTION,");
		sql.append("   MON$OLDEST_ACTIVE,");
		sql.append("   MON$OLDEST_SNAPSHOT,");
		sql.append("   MON$NEXT_TRANSACTION,");
		sql.append("   MON$PAGE_BUFFERS,");
		sql.append("   MON$SQL_DIALECT,");
		sql.append("   CASE MON$SHUTDOWN_MODE");
		sql.append("     WHEN 0 THEN '"+Language.getInstance().getProperties("MON$DATABASE.SHUTDOWN_MODE.ONLINE")+"'");
		sql.append("     WHEN 1 THEN '"+Language.getInstance().getProperties("MON$DATABASE.SHUTDOWN_MODE.MULTIUSERSHUTDOWN")+"'");
		sql.append("     WHEN 2 THEN '"+Language.getInstance().getProperties("MON$DATABASE.SHUTDOWN_MODE.SINGLEUSERSHUTDOWN")+"'");
		sql.append("     WHEN 3 THEN '"+Language.getInstance().getProperties("MON$DATABASE.SHUTDOWN_MODE.FULLSHUTDOWN")+"'");
		sql.append("   END AS MON$SHUTDOWN_MODE,");
		sql.append("   MON$SHUTDOWN_MODE,");
		sql.append("   MON$SWEEP_INTERVAL,");
		sql.append("   MON$READ_ONLY,");
		sql.append("   MON$FORCED_WRITES,");
		sql.append("   MON$RESERVE_SPACE,");
		sql.append("   MON$CREATION_DATE,");
		sql.append("   MON$PAGES,");
		sql.append("   CASE MON$BACKUP_STATE");
		sql.append("     WHEN 0 THEN  '"+Language.getInstance().getProperties("MON$DATABASE.BACKUP_STATE.NORMAL")+"'");
		sql.append("     WHEN 1 THEN  '"+Language.getInstance().getProperties("MON$DATABASE.BACKUP_STATE.STALLED")+"'");
		sql.append("     WHEN 2 THEN  '"+Language.getInstance().getProperties("MON$DATABASE.BACKUP_STATE.MERGE")+"'");
		sql.append("   END AS MON$BACKUP_STATE,");
		sql.append("   MON$STAT_ID ");
		sql.append("FROM MON$DATABASE");	
		
		statement = FBConnection.getInstance().getConnection().createStatement();		
		resultSet = statement.executeQuery(sql.toString());
		List<DataBase> list = new ArrayList<DataBase>();
        while (resultSet.next()){
        	list.add( new DataBase(
        			resultSet.getString("MON$DATABASE_NAME"),
        			resultSet.getInt("MON$PAGE_SIZE"),
        			resultSet.getInt("MON$ODS_MAJOR"),
        			resultSet.getInt("MON$ODS_MINOR"),
        			resultSet.getInt("MON$OLDEST_TRANSACTION"),
        			resultSet.getInt("MON$OLDEST_ACTIVE"),
        			resultSet.getInt("MON$OLDEST_SNAPSHOT"),
        			resultSet.getInt("MON$NEXT_TRANSACTION"),            
                    resultSet.getInt("MON$PAGE_BUFFERS"),
                    resultSet.getInt("MON$SQL_DIALECT"),            
                    resultSet.getString("MON$SHUTDOWN_MODE"),
                    resultSet.getInt("MON$SWEEP_INTERVAL"),
                    resultSet.getInt("MON$READ_ONLY"),
                    resultSet.getInt("MON$FORCED_WRITES"),
                    resultSet.getInt("MON$RESERVE_SPACE"),
                    resultSet.getDate("MON$CREATION_DATE"),
                    resultSet.getLong("MON$PAGES"),
                    resultSet.getString("MON$BACKUP_STATE"),
                    resultSet.getInt("MON$STAT_ID")                    
        			));
		}
		resultSet.getStatement().close();
		return list;
	}
}
